package ui;


import javax.swing.*;
import javax.swing.border.EmptyBorder;

import ui.handlers.OrderHandler;

import java.awt.*;
import java.rmi.RemoteException;

public class ConfirmOrderScreen extends BaseScreen{
	
	private JFrame mainFrame;
	private JButton confirm_btn, edit_btn;
	
	private int orderId;
	private OrderHandler placeOrderHandler;
	
	/* Methods needed for Handler */
	
	public JButton getConfirmButton()
	{ return confirm_btn; }
	
	public JButton getEditButton()
	{ return edit_btn; }
	
	public JFrame getMainFrame()
	{ return mainFrame; }
	
	public int getOrderId()
	{ return orderId; }
	
	public OrderHandler getPlaceOrderHandler(){
		return placeOrderHandler;
	}

	
	/* END OF HANDLER METHODS */
	
	public ConfirmOrderScreen(int orderId, OrderHandler placeOrderHandler)
	{
		this.orderId = orderId;
		this.placeOrderHandler = placeOrderHandler;
		
		mainFrame = new JFrame();
		mainFrame.setTitle("Confirm Order");
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setSize(650, 550);
		mainFrame.setLayout(new BorderLayout());
		
		confirm_btn = new JButton("Confirm");
		edit_btn = new JButton("Edit");
		
		doTitle();
		doMainContent();
		doButtons();
		
		mainFrame.setVisible(true);
	}
	
	private void doTitle()
	{
		String title = "<HTML><FONT SIZE = \"6\">" +
			"Confirm Order</FONT></HTML>";
		JLabel title_lbl = new JLabel(title);
		title_lbl.setBorder(new EmptyBorder(10, 10, 0, 0));
		mainFrame.add(title_lbl, BorderLayout.NORTH);
	}

	private void doMainContent()
	{		
		JPanel order_panel = new JPanel();
		String orderReceipt = "";
		try {
			orderReceipt = store.getPendingOrderToHtmlString(orderId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JLabel orderReceiptLabel = new JLabel(orderReceipt);
		order_panel.add(orderReceiptLabel);
		mainFrame.add(order_panel, BorderLayout.WEST);
	}
	
	private void doButtons()
	{
		JPanel button_panel = new JPanel();
		button_panel.setLayout(new FlowLayout());
		
		confirm_btn.addActionListener(new OrderHandler(this));
		edit_btn.addActionListener(new OrderHandler(this));
		
		JPanel confirm_panel = new JPanel();
		confirm_panel.add(confirm_btn);
		confirm_panel.setBorder(new EmptyBorder(0, 0, 0, 20));
		
		JPanel edit_panel = new JPanel();
		edit_panel.add(edit_btn);
		edit_panel.setBorder(new EmptyBorder(0, 10, 0, 10));
		
		JPanel cancel_panel = new JPanel();
		cancel_panel.setBorder(new EmptyBorder(0, 20, 0, 0));
		
		button_panel.add(confirm_panel);
		button_panel.add(edit_panel);
		button_panel.add(cancel_panel);
		button_panel.setBorder(new EmptyBorder(0, 0, 100, 0));
		
		mainFrame.add(button_panel, BorderLayout.SOUTH);
	}
	
}